package gui;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class insertstudent extends JFrame {
    private JPanel jPanel1 = new JPanel();
    private JLabel jLabel1 = new JLabel();
    private JLabel jLabel2 = new JLabel();
    private JTextField jTextField1 = new JTextField();
    private JTextField jTextField2 = new JTextField();
    private JTextField jTextField3 = new JTextField();
    private JLabel jLabel3 = new JLabel();
    private JLabel jLabel4 = new JLabel();
    private JButton jButton1 = new JButton();
    private JButton jButton2 = new JButton();
    private JButton jButton3 = new JButton();

    public insertstudent() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout( null );
        this.setSize(new Dimension(563, 365));
        this.setVisible(true);
        jPanel1.setBounds(new Rectangle(0, 0, 560, 335));
        jPanel1.setLayout(null);
        jLabel1.setText("jLabel1");
        jLabel1.setBounds(new Rectangle(-90, -95, 34, 14));
        jLabel2.setText("name :");
        jLabel2.setBounds(new Rectangle(115, 30, 85, 25));
        jTextField1.setBounds(new Rectangle(215, 35, 125, 20));
        jTextField2.setBounds(new Rectangle(215, 75, 125, 20));
        jTextField3.setBounds(new Rectangle(215, 120, 125, 20));
        jLabel3.setText("level :");
        jLabel3.setBounds(new Rectangle(115, 75, 35, 20));
        jLabel4.setText("department :");
        jLabel4.setBounds(new Rectangle(100, 120, 80, 20));
        jButton1.setText("insert");
        jButton1.setBounds(new Rectangle(105, 195, 75, 21));
        jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        jButton1_actionPerformed(e);
                    } catch (SQLException f) {
                        JOptionPane.showMessageDialog(null, " error "+f.getMessage());
                    }
                }
            });
        jButton2.setText("reset");
        jButton2.setBounds(new Rectangle(180, 195, 75, 21));
        jButton2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton2_actionPerformed(e);
                }
            });
        jButton3.setText("back");
        jButton3.setBounds(new Rectangle(255, 195, 75, 21));
        jButton3.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jButton3_actionPerformed(e);
                }
            });
        jPanel1.add(jButton3, null);
        jPanel1.add(jButton2, null);
        jPanel1.add(jButton1, null);
        jPanel1.add(jLabel4, null);
        jPanel1.add(jLabel3, null);
        jPanel1.add(jTextField3, null);
        jPanel1.add(jTextField2, null);
        jPanel1.add(jTextField1, null);
        jPanel1.add(jLabel2, null);
        jPanel1.add(jLabel1, null);
        this.getContentPane().add(jPanel1, null);
    }

    private void jButton1_actionPerformed(ActionEvent e) throws SQLException  {
        
        //login 
        
        if (jTextField1.getText().isEmpty() &&  jTextField2.getText().isEmpty()&&jTextField3.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "You Forgot Name And level and department  ");
        }else if(jTextField1.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, "You Forget student name");
        }else if(jTextField2.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, "You Forget student level ");
        }else if(jTextField3.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, "You Forget student department ");
        }
        else if(!jTextField1.getText().isEmpty()&& !jTextField2.getText().isEmpty() && !jTextField3.getText().isEmpty() )
            {
            database a=new database();
            String ins=" insert into students (name,level,department) values('"+jTextField1.getText()+"','"+jTextField2.getText()+"','"+jTextField3.getText()+"')";
            if(a.insert(ins)){
                JOptionPane.showMessageDialog(null, " student inserted successfully ");
            }else{
                JOptionPane.showMessageDialog(null, "error in insert ");
            }
            
            } 
    }

    private void jButton2_actionPerformed(ActionEvent e) {
        //reset button
        jTextField1.setText(" ");
        jTextField2.setText(" ");
        jTextField3.setText(" ");
        
        
    }

    private void jButton3_actionPerformed(ActionEvent e) {
        
        //back button
        real_project r=new real_project();
        this.setVisible(false);
    }
}
